package com.binc.testspring.study.mina.study1;

import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

/**
 * FileName: MyServer  mina编写的服务端
 * Autho: binC
 * Date:  2022/3/25 9:33
 */
@Slf4j
public class MyServer {

    static  int port = 9090;
    static IoAcceptor acceptor = null;

    public static void main(String[] args) throws IOException {
        try {

            acceptor = new NioSocketAcceptor();
            //设置编码过滤器
            acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(
                    new TextLineCodecFactory(
                            Charset.forName("GB18030"),
                            LineDelimiter.WINDOWS.getValue(),
                            // "Tx")
                            LineDelimiter.WINDOWS.getValue())
            ));
            acceptor.getSessionConfig().setReadBufferSize(20);
            acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
            // 注册自己自定义的业务处置对象
            acceptor.setHandler(new MyServerHandler());
            // 绑定完了就开始了服务端的监听操作；
            acceptor.bind(new InetSocketAddress(port));
           log.info("-----> 服务端启动，监听"+port+"端口");
        } catch (Exception e) {
            e.printStackTrace();
        }






    }


}
